package cn.sdormitory.smartdor.service.impl;

import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.smartdor.dao.SdDropOutDao;
import cn.sdormitory.smartdor.dao.SdRentDao;
import cn.sdormitory.smartdor.entity.SdDropOut;
import cn.sdormitory.smartdor.entity.SdRent;
import cn.sdormitory.smartdor.service.SdDropOutService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class SdDropOutServiceImpl extends ServiceImpl<SdDropOutDao,SdDropOut> implements SdDropOutService {

    @Autowired
    SdDropOutDao sdDropOutDao;

    @Autowired
    BStudentDao bStudentDao;

    @Autowired
    SdRentDao sdRentDao;


    @Transactional
    public CommonResult dropOutDorm(SdDropOut sdDropOut){
        //学生退宿
        // 1.修改住宿状态
        QueryWrapper<BStudent> stuWrapper = new QueryWrapper<>();
        stuWrapper.eq("studentNo",sdDropOut.getStudentNo());
        BStudent bStudent = bStudentDao.selectOne(stuWrapper);
        if(Integer.valueOf(bStudent.getStatus()) == 0){
            return CommonResult.failed("走读生无法进行退宿！！！");
        }
        bStudent.setStatus("0");
        //2.修改住宿费用表信息
        QueryWrapper<SdRent> wrapper = new QueryWrapper<>();
        wrapper.eq("studentNo",sdDropOut.getStudentNo());
        SdRent sdRent = sdRentDao.selectOne(wrapper);
        sdRent.setDeposit(null);
        sdRent.setPayStatus(null);
        //3.插入本次退宿信息
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        sdDropOut.setRefundDate(simpleDateFormat.format(new Date()));
        sdDropOutDao.insert(sdDropOut);
        return CommonResult.success("退宿成功！！！");
    }
}
